<?php
/**
 * 工地每日回复和修改
 * User: Administrator
 * Date: 2019/4/27/027
 * Time: 17:50
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\OrderBindingTag;
use app\Lib\Model\DbModel\OrderLog;
use app\Lib\Model\DbModel\OrderLogFile;
use app\Lib\Model\User\User;

class OperatingOrderLog
{
    private $order;
    private $orderLog = 0;
    private $imgArray = [];
    private $content;
    private $logTable;
    private $logTableFile;
    private $user;
    public function __construct(Order $order,User $user)
    {
        $this->order = $order;
        $this->logTable = new OrderLog();
        $this->logTableFile = new OrderLogFile();
        $this->user = $user;
    }

    /**
     * @param mixed $orderLog
     */
    public function setOrderLog($orderLog)
    {
        $this->orderLog = $orderLog;
    }

    /**
     * @param mixed $imgArray
     */
    public function setImgArray($imgArray)
    {
        $this->imgArray = $imgArray;
    }

    /**
     * @param mixed $content
     */
    public function setContent($content)
    {
        $this->content = $content;
    }

    /**
     * 模拟数据
     *  新增
     *     [
     *      'order_id'=>1,
     *      'content'=>'这是个测试'
     *      'opUid'=>1
     *      'log_id'=>1
     *      'file'=>[
     *          'img'=>[
     *              [
     *                 'time_id'=>1
     *                  'img_id'=>33
     *              ]
     *              [
     *                  'time_id'=>0
     *                  'img_id'=>33
     *              ]
     *          ]
     *       ]
     *      ]
     */
    /**
     * @throws BlException
     */
    public function run(){
        if(!$this->order->is_underconstruction_status()){
            throw new BlException('工单还没开工');
        }
        if($this->orderLog > 0){
            $this->editOrderLog();
        }else{
            $this->addOrderLog();
        }
        if(!empty($this->imgArray) && is_array($this->imgArray)){
            foreach ($this->imgArray as $v){
                if($v['time_id']>0){
                    $this->editOrderImg($v);
                }else{
                    $this->addOrderImg($v);
                }
            }
        }
        //取消停工标签
        $this->delStopWord();
        return true;
    }

    /**
     * 新增文字回复
     * @throws BlException
     */
    private function addOrderLog(){
        $logTable = $this->logTable ;
        if($this->order->is_hand_stop_word()){
            $this->content =  '【工地已正常开工,未处于停工状态】:'.$this->content;
        }
        $data = [
            $logTable->_order_id=>$this->order->get_order_id(),
            $logTable->_create_time=>time(),
            $logTable->_create_uid=>$this->user->getUid(),
            $logTable->_content=>$this->content
        ];
        $result = $logTable->insert($data,false,true);
        if(!$result){
            throw new BlException('新增工地日志失败');
        }
        $this->orderLog = $result;
    }

    /**
     * 修改文字回复
     * @throws BlException
     */
    private function editOrderLog(){
        $logTable = $this->logTable ;
        $data = [
            $logTable->_update_time=>time(),
            $logTable->_update_uid=>$this->user->getUid(),
            $logTable->_content=>$this->content
        ];
        $result = $logTable->update($data);
        if($result === false){
            throw new BlException('修改工地日志失败');
        }
    }

    /**
     * 新增图片回复
     * @param $imgData
     * @throws BlException
     */
    private function addOrderImg($imgData){
        $logTableFile = $this->logTableFile;
        $data = [
            $logTableFile->_img_id=>$imgData['img_id'],
            $logTableFile->_order_log_id=>$this->orderLog,
        ];
        $result = $logTableFile->insert($data);
        if(!$result){
            throw new BlException('上传图片失败');
        }
    }

    /**
     * 修改图片回复
     * @param $imgData
     * @throws BlException
     */
    private function editOrderImg($imgData){
        $logTableFile = $this->logTableFile;
        $data = [
            $logTableFile->_img_id=>$imgData['img_id'],
            $logTableFile->_order_log_id=>$this->orderLog,
            $logTableFile->_id=>$imgData['time_id'],
        ];
        $result = $logTableFile->update($data);
        if($result === false){
            throw new BlException('修改图片失败');
        }
    }

    /**
     * 删除标签
     * @return bool
     * @throws BlException
     */
    private function delStopWord(){
        $model = $this->order->is_hand_stop_word();
        $result = true;
        if($model){
           $modelStop =new OrderBindingTag();
           $result = $modelStop
               ->where([$modelStop->_order_id=>$this->order->get_order_id()])
               ->update([$modelStop->_status=>-1,$modelStop->_end_time=>time()]);
            if($result ===false){
                throw new BlException('删除停工标签失败');
            }
            (new OrderPush($this->order))->startWordPush();
        }
        return true;
    }
}